<?php
class Model_Core_Abstract
    extends Object
{
    protected $_resourceModel;

    protected $_primary;

    protected $_new = true;

    protected $_change = false;

    protected function _init($resourceModel)
    {
        if(!isset($resourceModel)) {
            throw new Zend_Exception('No set resource model in '.get_class($this));
        } elseif(is_string($resourceModel)) {
            $this->_resourceModel = new $resourceModel();
        } else {
            $this->_resourceModel = $resourceModel;
        }

        return $this;
    }

    public function setPrimary($value)
    {
        $this->_primary = $value;
        return $this;
    }

    public function getId()
    {
        if(isset($this->_primary)) {
            return $this->getData($this->_primary);
        } else {
            return $this->getId();
        }
    }

    public function getResource()
    {
        return $this->_resourceModel;
    }

    public function load($value,$field=null)
    {
        $this->_beforeLoad();
        $this->getResource()->load($value,$this,$field);
        $this->_afterLoad();
        return $this;
    }

    protected function _beforeLoad()
    {
        return $this;
    }

    protected function _afterLoad()
    {
        return $this;
    }

    public function setOriginalData($key, $value=null)
    {
        parent::setData($key,$value);
        $this->_new = false;
    }

    public function setData($key, $value=null)
    {
        parent::setData($key,$value);
        $this->_change = true;

        return $this;
    }

    public function getNew()
    {
        return $this->_new;
    }

    public function getChange()
    {
        return $this->_change;
    }

    public function setNew($flag)
    {
        $this->_new = $flag;
        return $this;
    }

    public function setChange($flag)
    {
        $this->_change = $flag;
        return $this;
    }

    public function save()
    {
        $this->getResource()->save($this);
    }

    public function delete()
    {
        $this->getResource()->deleteModel($this);
    }
}